<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="停车点编号" prop="stopno">
        <el-input
          v-model="queryParams.stopno"
          placeholder="请输入停车点编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="停车点名称" prop="stopname">
        <el-input
          v-model="queryParams.stopname"
          placeholder="请输入停车点名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所在政区" prop="belong">
        <el-select v-model="queryParams.belong" placeholder="请选择所在政区" clearable>
          <el-option value="南京市">南京市</el-option>
          <el-option value="无锡市">无锡市</el-option>
          <el-option value="徐州市">徐州市</el-option>
          <el-option value="常州市">常州市</el-option>
          <el-option value="苏州市">苏州市</el-option>
          <el-option value="南通市">南通市</el-option>
          <el-option value="连云港市">连云港市</el-option>
          <el-option value="淮安市">淮安市</el-option>
          <el-option value="盐城市">盐城市</el-option>
          <el-option value="扬州市">扬州市</el-option>
          <el-option value="镇江市">镇江市</el-option>
          <el-option value="泰州市">泰州市</el-option>
          <el-option value="宿迁市">宿迁市</el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="stopList" @selection-change="handleSelectionChange">
      <!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column label="ID" align="center" prop="id"/>
      <el-table-column label="停车点编号" align="center" prop="stopno"/>
      <el-table-column label="停车点名称" align="center" prop="stopname"/>
      <el-table-column label="启用日期" align="center" prop="builddate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.builddate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="总容量/位" align="center" prop="capacity"/>
      <el-table-column label="投放量/辆" align="center" prop="size"/>
      <el-table-column label="政区名称" align="center" prop="cityname">
        <template slot-scope="scope">
          {{ scope.row.city.cityname }}
        </template>
      </el-table-column>
      <el-table-column label="政区编号" align="center" prop="belong"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-thumb"
            @click="handleUpdate(scope.row)"
          >投放单车
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--    <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />-->

    <!-- 添加或投放对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <div v-if="title=='添加'">
          <el-form-item label="停车点编号" prop="stopno">
            <el-input v-model="form.stopno" placeholder="请输入停车点编号"/>
          </el-form-item>
          <el-form-item label="停车点名称" prop="stopname">
            <el-input v-model="form.stopname" placeholder="请输入停车点名称"/>
          </el-form-item>
          <el-form-item label="启用日期" prop="builddate">
            <el-date-picker
              v-model="form.builddate"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="请选择启用日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="总容量" prop="capacity">
            <el-input v-model="form.capacity" placeholder="请输入数字"/>
          </el-form-item>
          <el-form-item label="所在政区" prop="belong">
            <el-select v-model="form.belong" placeholder="请选择所在政区" clearable>
              <el-option value="南京市">南京市</el-option>
              <el-option value="无锡市">无锡市</el-option>
              <el-option value="徐州市">徐州市</el-option>
              <el-option value="常州市">常州市</el-option>
              <el-option value="苏州市">苏州市</el-option>
              <el-option value="南通市">南通市</el-option>
              <el-option value="连云港市">连云港市</el-option>
              <el-option value="淮安市">淮安市</el-option>
              <el-option value="盐城市">盐城市</el-option>
              <el-option value="扬州市">扬州市</el-option>
              <el-option value="镇江市">镇江市</el-option>
              <el-option value="泰州市">泰州市</el-option>
              <el-option value="宿迁市">宿迁市</el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="位置[区域边界]" prop="geom">
            <el-input v-model="form.geom" autosize type="textarea"/>
          </el-form-item>
        </div>
        <div v-else>
          <el-form-item label="投放点id" prop="stopid">
            <el-input v-model="form.stopid" disabled/>
          </el-form-item>
          <el-form-item label="投放点名" prop="stopname">
            <el-input v-model="form.stopname" disabled/>
          </el-form-item>
          <el-form-item label="所属政区" prop="cityname">
            <el-input v-model="form.cityname" disabled/>
          </el-form-item>
          <el-form-item label="可用容量" prop="count">
            <el-input v-model="form.count" disabled/>
          </el-form-item>
          <el-form-item label="单车类型" prop="typeid">
            <el-select v-model="form.typeid" placeholder="请选择单车类型" clearable>
              <el-option
                v-for="dict in bikeTypeOptions"
                :key="dict.id"
                :label="dict.type"
                :value="dict.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="投放数量" prop="addtion">
            <el-input-number :min="1" v-model="form.addtion"/>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listStop, getBikeType, addStop, putInStop} from "@/api/bike/stop";

export default {
  name: "Stop",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      stopList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        stopno: null,
        stopname: null,
        builddate: null,
        capacity: null,
        size: null,
        belong: null,
        geom: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        stopno: [
          {required: true, message: "停车点编号不能为空", trigger: "blur"}
        ],
        stopname: [
          {required: true, message: "停车点名称不能为空", trigger: "blur"}
        ],
      },
      bikeTypeOptions: []
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getBikeType() {
      getBikeType().then(response => {
        this.bikeTypeOptions = response.data;
      });
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      listStop(this.queryParams).then(response => {
        this.loading = false;
        this.stopList = response.data;
        this.total = response.data.length;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.geom = "{\"type\":\"Polygon\",\"coordinates\":[[[118.635263,33.096575],[118.635263,33.106785],[118.648825,33.106785],[118.648825,33.096575],[118.635263,33.096575]]]}"

      this.open = true;
      this.title = "添加";
    },
    /** 投放按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form.stopid = row.id
      this.form.stopname = row.stopname
      this.form.cityname = row.city.cityname
      this.form.count = row.capacity - row.size
      this.getBikeType()
      this.open = true;
      this.title = "投放";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.stopid != null) {
            this.form.username = 'admin2'
            this.form.reduce = 0
            delete this.form.stopname
            delete this.form.cityname
            delete this.form.count
            putInStop(this.form).then(response => {
              this.$modal.msgSuccess("投放成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStop(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    del(row) {
      this.stopList = this.stopList.filter(i => {
        return i.id !== row.id
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除编号为"' + ids + '"的数据项？').then(function () {
        return
      }).then(() => {
        this.del(row)
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
  }
};
</script>
